{extend name="layout" /}
{block name="page-header"}
<h5><i class="fa fa-fw fa-connectdevelop"></i>  {:lang('数据备份')}</h5>
{/block}
{block name="content"}
<div class="row">
    <div class="col-xs-12">
	<div class="box">
		<div class="box-header with-border">
			<h3 class="box-title">{$page_title}</h3>
		</div>
		<div class="box-body">
        <div class="mb10">
            <a id="export" class="btn btn-sm mr5 btn-default " href="javascript:;" autocomplete="off"><i class="fa fa-cube"></i> {:lang('立即备份')}</a>
            <a class="btn btn-sm mr5 btn-default  ajax-post confirm" href="{:url('optimize')}" target-form="ids"><i class="fa fa-heartbeat"></i> {:lang('优化表')}</a>
            <a class="btn btn-sm mr5 btn-default  ajax-post confirm" href="{:url('repair')}" target-form="ids"><i class="fa fa-gavel"></i> {:lang('修复表')}</a>
			<a class="btn btn-sm mr5 btn-default " href="{:url('index', ['type' => 'import'])}" autocomplete="off"><i class="fa fa-history"></i> {:lang('还原表')}</a>
			{eq name="UID" value="1"}
			<a class="btn btn-sm mr5 btn-default " href="{:url('run_sql')}" autocomplete="off"><i class="fa fa-code"></i> {:lang('运行')}SQL{:lang('语句')}</a>
			{/eq}
        </div>

        <!-- 应用列表 -->
        <div class="data-table table-striped">
            <form id="export-form" method="post" action="{:url('export')}">
                <table class="table table-bordered table-striped table-hover">
                    <thead>
                        <tr>
                            <th width="48"><input class="checkboxCtrl" data-group="tables[]" data-toggle="icheck" checked="chedked" type="checkbox">
</th>
                            <th>{:lang('表名')}</th>
                            <th width="120">{:lang('数据量')}</th>
                            <th width="120">{:lang('数据大小')}</th>
                            <th width="220" class="text-center">{:lang('创建时间')}</th>
                            <th width="150" class="text-center">{:lang('备份状态')}</th>
                            <th width="200" class="text-center">{:lang('操作')}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {volist name="list" id="table"}
                            <tr>
                                <td class="num">
                                    <input class="ids" data-toggle="icheck" checked="chedked" type="checkbox" name="tables[]" value="{$table.name}">
                                </td>
                                <td>{$table.name}</td>
                                <td>{$table.rows}</td>
                                <td>{$table.data_length|format_bytes}</td>
                                <td class="text-center">{$table.create_time}</td>
                                <td id="info" class="text-center">{:lang('未备份')}</td>
                                <td class="action text-center">
                                    <a class="ajax-get btn btn-xs btn-default " href="{:url('optimize?tables='.$table['name'])}"><i class="fa fa-heartbeat"></i> {:lang('优化表')}</a>
                                    <a class="ajax-get btn btn-xs btn-default " href="{:url('repair?tables='.$table['name'])}"><i class="fa fa-gavel"></i> {:lang('修复表')}</a>
                                </td>
                            </tr>
                        {/volist}
                    </tbody>
                </table>
            </form>
        </div>
		</div>
    <!-- /应用列表 -->
	</div>
    </div>
</div>
{/block}

{block name="script"}
    <script type="text/javascript">
    (function($){
        var $form = $("#export-form"), $export = $("#export"), tables

        $export.click(function(){
            $export.parent().children().addClass("disabled");
            $export.html("{:lang('正在发送备份请求')}"+"...");
            $.post(
                $form.attr("action"),
                $form.serialize(),
                function(data){
                    if(data.code){
                        tables = data.data.tables;
                        $export.html(data.msg + "{:lang('开始备份')}"+"，"+"{:lang('请不要关闭本页面')}"+"！");
                        backup(data.data.tab);
                        window.onbeforeunload = function(){ return "{:lang('正在备份数据库')}"+"，"+"{:lang('请不要关闭')}"+"！" }
                    } else {
                        layer.msg(data.msg, {icon: 5});
                        $export.parent().children().removeClass("disabled");
                        $export.html("立即备份");
                        setTimeout(function(){
        	                $('#top-alert').find('button').click();
        	                $(this).removeClass('disabled').prop('disabled',false);
        	            },1500);
                    }
                },
                "json"
            );
            return false;
        });

        function backup(tab, code){
            code && showmsg(tab.id, "{:lang('开始备份')}"+"...(0%)");
            $.get($form.attr("action"), tab, function(data){
                if(data.code){
                    showmsg(tab.id, data.msg);
                    if(!$.isPlainObject(data.data.tab)){
                        $export.parent().children().removeClass("disabled");
                        $export.html("{:lang('备份完成')}"+"，"+"{:lang('点击重新备份')}");
                        window.onbeforeunload = function(){ return null }
                        return;
                    }
                    backup(data.data.tab, tab.id != data.data.tab.id);
                } else {
                    layer.msg(data.msg, {icon: 5});
                    $export.parent().children().removeClass("disabled");
                    $export.html("{:lang('立即备份')}");
                    setTimeout(function(){
    	                $('#top-alert').find('button').click();
    	                $(that).removeClass('disabled').prop('disabled',false);
    	            },1500);
                }
            }, "json");

        }

        function showmsg(id, msg){
            $form.find("input[value=" + tables[id] + "]").closest("tr").find("#info").html(msg);
        }
    })(jQuery);
    </script>
{/block}